﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    class Alms_DAl : IAlms
    {
        #region IAlms 成员

        private db.ParameterArry[] ItemsParamer(Alms_Model md)
        {
            db.ParameterArry[] items = new db.ParameterArry[12];
            items[0].dbType = SqlDbType.Int;
            items[0].objValue = md.Id;
            items[0].parName = "@id";

            items[1].dbType = SqlDbType.DateTime;
            items[1].objValue = StringHelper.isDatetime(md.Addtime);
            items[1].parName = "@Addtime";

            items[2].dbType = SqlDbType.Money;
            items[2].objValue = md.Almsmoney;
            items[2].parName = "@Almsmoney";

            items[3].dbType = SqlDbType.Int;
            items[3].objValue = md.AmsType;
            items[3].parName = "@AmsType";


            items[4].dbType = SqlDbType.VarChar;
            items[4].objValue = md.ChannelId;
            items[4].parName = "@ChannelId";


            items[5].dbType = SqlDbType.VarChar;
            items[5].objValue = md.Email;
            items[5].parName = "@Email";


            items[6].dbType = SqlDbType.VarChar;
            items[6].objValue = md.NetIP;
            items[6].parName = "@NetIP";

            items[7].dbType = SqlDbType.VarChar;
            items[7].objValue = md.Readme;
            items[7].parName = "@Readme";


            items[8].dbType = SqlDbType.VarChar;
            items[8].objValue = md.TrueName;
            items[8].parName = "@TrueName";

            items[9].dbType = SqlDbType.Int;
            items[9].objValue = md.TypeId;
            items[9].parName = "@TypeId";

            items[10].dbType = SqlDbType.Int;
            items[10].objValue = md.UserId;
            items[10].parName = "@UserId";

            items[11].dbType = SqlDbType.Int;
            items[11].objValue = md.State;
            items[11].parName = "@State";



            return items;
        }
        public DataTable GetDataTable(string filter)
        {
            DataTable dt = new DataTable();
            db Conn = new db();
            if (filter + "" != "")
            {
                filter = " where " + filter;
            }
            string Sqlstr = "Select * from Alms " + filter;
            dt = Conn.GetSql_DataTable(Sqlstr);
            return dt;
        }

        public Alms_Model GetModel(DataRow dr)
        {
            Alms_Model md = new Alms_Model();
            if (dr["ID"] != null)
            {
                md.Id = StringHelper.isNumber_int(dr["id"].ToString());
            }

            if (dr["TrueName"] != null)
            {
                md.TrueName = dr["TrueName"].ToString();
            }


            if (dr["Email"] != null)
            {
                md.Email = dr["Email"].ToString();
            }




            if (dr["UserId"] != null)
            {
                md.UserId = StringHelper.isNumber_int(dr["UserId"].ToString());
            }

            if (dr["NetIP"] != null)
            {
                md.NetIP = dr["NetIP"].ToString();
            }

            if (dr["ChannelId"] != null)
            {
                md.ChannelId = dr["ChannelId"].ToString();
            }

            if (dr["TypeId"] != null)
            {
                md.TypeId = StringHelper.isNumber_int(dr["TypeId"].ToString());
            }

            if (dr["Readme"] != null)
            {
                md.Readme = dr["Readme"].ToString();
            }

            if (dr["Almsmoney"] != null)
            {
                try
                {
                    md.Almsmoney = Convert.ToDouble(dr["Almsmoney"].ToString());
                }
                catch
                {
                    md.Almsmoney = 0;
                }
            }


            if (dr["AmsType"] != null)
            {
                md.AmsType = StringHelper.isNumber_int(dr["AmsType"].ToString());
            }

            if (dr["AddTime"] != null)
            {
                md.Addtime = dr["AddTime"].ToString();
            }

            if (dr["state"] != null)
            {
                md.State = StringHelper.isNumber_int(dr["state"].ToString());
            }




            return md;
        }

        public int Add(Alms_Model md)
        {
            int result = 0;
            db ConnInsert = new db();

            db.ParameterArry[] ParItems = ItemsParamer(md);
            try
            {
                string sqlstr = "Insert into Alms(TrueName,Email,UserId,NetIP,ChannelId,TypeId,Readme,Almsmoney,AmsType,state)";
                sqlstr += " values(@TrueName,@Email,@UserId,'" + StringHelper.Ip() + "',@ChannelId,@TypeId,@Readme,@Almsmoney,@AmsType,@state)";
                result = ConnInsert.ExcSql(sqlstr, ParItems);
            }
            catch
            {
                result = -1;
            }

            return result;


        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int Delete(string filter)
        {
            string cmdText = "delete from Alms where {0}";
            cmdText = string.Format(cmdText,filter);
            db dbclass = new db();
            int count = 0;
            count = dbclass.ExcSql(cmdText);
            return count;
        }

        /// <summary>
        /// 设置支付
        /// </summary>
        /// <param name="isPay"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int setPay(int isPay, string filter)
        {
            string cmdText = "update Alms set state={0}  where {1}";
            cmdText = string.Format(cmdText, isPay, filter);
            db dbclass = new db();
            int count = 0;
            count = dbclass.ExcSql(cmdText);
            return count;
        }

        #endregion
    }
}
